//
//  MainViewController.swift
//  weibo
//
//  Created by ForeverOSS on 16/2/6.
//  Copyright © 2016年 foreveross. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        addChildViewController()

        setupComposedButton()
    }
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        tabBar.bringSubviewToFront(composedButton)
    }
    
    ///设置撰写按钮位置
    private func setupComposedButton(){
    
    
        let count = childViewControllers.count
        
        let w = tabBar.bounds.width / CGFloat(count) - 1
        
        composedButton.frame = CGRectInset(tabBar.bounds, 2 * w, 0)
        
        tabBar.addSubview(composedButton)
    
        //添加按钮监听事件
        composedButton.addTarget(self, action: "clickComposedButton", forControlEvents: .TouchUpInside)
    }
    
  //MARK: - 监听方法
    @objc private func clickComposedButton(){
    
        printLog("点击撰写按钮")
    }
    
  //MARK: - 懒加载控件
    //撰写按钮
    private lazy var composedButton: UIButton = UIButton(imageName: "tabbar_compose_icon_add", backImageName: "tabbar_compose_button")
    
    
    /// 添加指定的控制器
    ///
    /// - parameter vc:        控制器
    /// - parameter title:     标题
    /// - parameter imageName: 图像名
    private func addChildViewController() {
        
        tabBar.tintColor = UIColor.orangeColor()
        
        addChildViewController(HomeViewControlle(), title: "首页", imageName: "tabbar_home")
        
        addChildViewController(MessageViewController(), title: "消息", imageName: "tabbar_message_center")
        
        addChildViewController(UIViewController())
        
        addChildViewController(DiscoverViewController(), title: "发现", imageName: "tabbar_discover")
        
        addChildViewController(ProfileViewController(), title: "我", imageName: "tabbar_profile")
        
    }
    // 添加所有子控制器
    private func addChildViewController(vc:UIViewController ,title:String , imageName:String) {
        
        vc.title = title
        
        vc.tabBarItem.image = UIImage(named: imageName)

        
        addChildViewController(UINavigationController(rootViewController: vc))
        

    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
